package cn.edu.huas.shopping.controller;

import cn.edu.huas.shopping.pojo.Cart;
import cn.edu.huas.shopping.pojo.CartItem;
import cn.edu.huas.shopping.pojo.Product;
import cn.edu.huas.shopping.pojo.ResponseResult;
import cn.edu.huas.shopping.service.ProductService;
import cn.edu.huas.shopping.service.ProductServiceImpl;
import cn.edu.huas.shopping.utils.JsonUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import javax.swing.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Objects;

@WebServlet("*.cart")
public class CartManagerAction extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //获取到请求路径
        String servletPath = request.getServletPath();
        ProductService productService=new ProductServiceImpl();
        //获取session
        HttpSession session= request.getSession();
        PrintWriter out=response.getWriter();

        if ("/add.cart".equals(servletPath)){
            String pidStr = request.getParameter("id");
            String qtyStr = request.getParameter("qty");
            Integer pid=Integer.parseInt(pidStr);
            Integer qty=Integer.parseInt(qtyStr);
            Product product=productService.loadOneProduct(pid);
            //尝试从购物车中获取购物车对象
            Cart cart= (Cart) session.getAttribute("cart");
            if (Objects.isNull(cart)){
                cart=new Cart();
                session.setAttribute("cart",cart);
            }
            cart.addItem(product,qty);
            ResponseResult<String> rs=new ResponseResult<>(true,"添加成功",20051);
            out.println(JsonUtils.objectToJson(rs));
        }else if("/load.cart".equals(servletPath)){
            //加载购物车数据
            Cart cart= (Cart) session.getAttribute("cart");
            ResponseResult<Cart> cartResponseResult = new ResponseResult<>();
            if (Objects.isNull(cart)){
                cartResponseResult=new ResponseResult<>(false,"购物车不存在",50051);
            }else {
                cartResponseResult=new ResponseResult<Cart>(true,"购物车存在",200252,cart);
            }
            out.println(JsonUtils.objectToJson(cartResponseResult));
        }else if("/delete.cart".equals(servletPath)){

            //1.先获取到id
            String pidStr = request.getParameter("id");
            Integer id=Integer.parseInt(pidStr);

            //2.尝试从购物车中删除数据
            Cart cart= (Cart) session.getAttribute("cart");
            cart.deleteItem(id);
            ResponseResult<Cart> rs=new ResponseResult<Cart>(true,"删除成功",20051,cart);
            System.out.println(JsonUtils.objectToJson(rs));
            out.println(JsonUtils.objectToJson(rs));
        }else if ("/upData.cart".equals(servletPath)){
            //1.先获取商品的id以及商品增加的数量
            String pidStr = request.getParameter("id");
            String qtyStr = request.getParameter("qty");
            Integer pid=Integer.parseInt(pidStr);
            Integer qty=Integer.parseInt(qtyStr);
            //2.尝试从购物车中获取购物车对象
            Cart cart= (Cart) session.getAttribute("cart");
            cart.updataItem(pid,qty);
            ResponseResult<String> rs=new ResponseResult<>(true,"跟新成功!",20051);
            out.println(JsonUtils.objectToJson(rs));
        }
    }
}
